﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autofac;
using ComputerVision.Data;
using ComputerVision.Services;

namespace ComputerVision.UserControls
{
    public partial class ucBaoCaoDoanhThu : UserControl
    {
        private readonly IService<LoaiVe> _loaiVeXeService;
        private readonly IService<LoaiXe> _loaiXeXeService;
        private Guid guid = Guid.Parse("24063092-d674-4ee0-937b-540659da4cd1");
        private IService<Xe> _xe;
        private int type = 0;

        public ucBaoCaoDoanhThu()
        {
            InitializeComponent();
            _loaiVeXeService = Program.Container.Resolve<IService<LoaiVe>>();
            _loaiXeXeService = Program.Container.Resolve<IService<LoaiXe>>();
            _xe = Program.Container.Resolve<IService<Xe>>();
            LoadLoaiVe();
            LoadLoaiXe();
        }

        private void LoadLoaiVe()
        {
            var loaiVe = new LoaiVe()
            {
                Id = 0,
                TenLoai = "Tất cả",
                VeXe = null

            };

            var items = _loaiVeXeService.ToList();
            items.Insert(0, loaiVe);
            cbLoaiVe.DataSource = items;
            cbLoaiVe.DisplayMember = "TenLoai";
            cbLoaiVe.ValueMember = "Id";
            cbLoaiVe.SelectedItem = cbLoaiVe.Items[0];
        }

        private void LoadLoaiXe()
        {
            var loaiXe = new LoaiXe()
            {
                Id = guid,
                Ten = "Tất cả"

            };

            var items = _loaiXeXeService.ToList();
            items.Insert(0, loaiXe);
            cbLoaiXe.DataSource = items;
            cbLoaiXe.DisplayMember = "Ten";
            cbLoaiXe.ValueMember = "Id";
            cbLoaiXe.SelectedItem = cbLoaiXe.Items[0];
        }

        private void btTimkiem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            switch (type)
            {
                case 0:
                    if ((int)cbLoaiVe.SelectedValue == 0 && (Guid)cbLoaiXe.SelectedValue == guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date == DateTime.Now.Date).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else if ((int)cbLoaiVe.SelectedValue != 0 && (Guid)cbLoaiXe.SelectedValue == guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date == DateTime.Now.Date && p.VeXe.LoaiVeId == (int)cbLoaiVe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else if ((int)cbLoaiVe.SelectedValue == 0 && (Guid)cbLoaiXe.SelectedValue != guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date == DateTime.Now.Date && p.LoaiXeId == (Guid)cbLoaiXe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date == DateTime.Now.Date && p.LoaiXeId == (Guid)cbLoaiXe.SelectedValue && p.VeXe.LoaiVeId == (int)cbLoaiVe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    break;
                case 1:
                    if ((int)cbLoaiVe.SelectedValue == 0 && (Guid)cbLoaiXe.SelectedValue == guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= DateTime.Today.AddDays(-7) &&
                       p.NgayVao.Value.Date <= DateTime.Today).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else if ((int)cbLoaiVe.SelectedValue != 0 && (Guid)cbLoaiXe.SelectedValue == guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= DateTime.Today.AddDays(-7) &&
                       p.NgayVao.Value.Date <= DateTime.Today && p.VeXe.LoaiVeId == (int)cbLoaiVe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else if ((int)cbLoaiVe.SelectedValue == 0 && (Guid)cbLoaiXe.SelectedValue != guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= DateTime.Today.AddDays(-7) &&
                       p.NgayVao.Value.Date <= DateTime.Today && p.LoaiXeId == (Guid)cbLoaiXe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= DateTime.Today.AddDays(-7) &&
                       p.NgayVao.Value.Date <= DateTime.Today && p.LoaiXeId == (Guid)cbLoaiXe.SelectedValue && p.VeXe.LoaiVeId == (int)cbLoaiVe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }

                    break;
                case 2:
                    if ((int)cbLoaiVe.SelectedValue == 0 && (Guid)cbLoaiXe.SelectedValue == guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= dtRaTu.Value.Date &&
                       p.NgayVao.Value.Date <= dtRaDen.Value.Date).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else if ((int)cbLoaiVe.SelectedValue != 0 && (Guid)cbLoaiXe.SelectedValue == guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= dtRaTu.Value.Date &&
                       p.NgayVao.Value.Date <= dtRaDen.Value.Date && p.VeXe.LoaiVeId == (int)cbLoaiVe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else if ((int)cbLoaiVe.SelectedValue == 0 && (Guid)cbLoaiXe.SelectedValue != guid)
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= dtRaTu.Value.Date &&
                       p.NgayVao.Value.Date <= dtRaDen.Value.Date && p.LoaiXeId == (Guid)cbLoaiXe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }
                    else
                    {
                        var items =
                        _xe.ToList()
                           .Where(
                               p =>
                               p.NgayVao != null && p.NgayVao.Value.Date >= dtRaTu.Value.Date &&
                       p.NgayVao.Value.Date <= dtRaDen.Value.Date && p.LoaiXeId == (Guid)cbLoaiXe.SelectedValue && p.VeXe.LoaiVeId == (int)cbLoaiVe.SelectedValue).Select(a => new { a.Id, a.SoVe, a.BienSoXe, a.NgayVao, a.NgayRa, LoaiXe = a.LoaiXe.Ten, LoaiVe = a.VeXe.LoaiVe.TenLoai, a.GiaTien, a.Created, a.CreatedBy, a.Modified, a.ModifiedBy }).ToList();


                        lbKetQua.Text = items.Count().ToString();
                        lblTongTien.Text = decimal.Parse(items.Sum(a => a.GiaTien).ToString()).ToString("n0");
                        xeBindingSource.DataSource = items.OrderByDescending(p => p.NgayVao);
                    }

                    break;
            }
            Cursor.Current = Cursors.Default;
        }

        private void rbTrongNgay_CheckedChanged(object sender, EventArgs e)
        {
            type = 0;
        }

        private void rbTrongTuan_CheckedChanged(object sender, EventArgs e)
        {
            type = 1;
        }

        private void rbThoiGianTu_CheckedChanged(object sender, EventArgs e)
        {
            type = 2;
        }
    }
}
